package com.woniuxy.enjoylife.utils;


import com.woniuxy.enjoylife.entity.Log;
import com.woniuxy.enjoylife.mapper.LogMapper;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.crypto.interfaces.PBEKey;
import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;

@Configuration
@EnableScheduling
public class LogUtil {
    @Resource
    private RedisTemplate<String, Log> redisTemplate;
    @Resource
    private LogMapper logMapper;


    @Scheduled(cron = "10/10 * * * * ?")
    public void run() {
        List<Log> logList = new ArrayList<>();
        while (redisTemplate.opsForList().size("logList") != 0) {
            Log log = redisTemplate.opsForList().leftPop("logList");
            logList.add(log);
        }
        if (logList.size() > 0) {
            logMapper.insertLogs(logList);
        }
    }
}